﻿package Naruto {	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.Loader;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	
	import flash.display.Shape;
	
	import Naruto.Images.*;

	public class LoadPic extends Sprite
	{
		private var _stage:Stage;
		private var _picNum:int;
		private var _loader:Loader = new Loader();
		private var _maskImgLoader:Loader;
		private var zhezhao : Sprite = null;
		
		//按钮
		private var _playAgain:PlayAgain;
		private var _menu:Menu;
		private var _playAgainName = '';
		private var _menuName = '';
		
		public function LoadPic(stage:Stage, num:int):void
		{
			this._picNum = num;			
			this._stage = stage;
			startLoadPic(num);
		}
		public function startLoadPic(num:int){
			this._picNum = num;
			
			if(num<300){		//背景或地图
				this._loader.load(new URLRequest('Naruto/Images/bg' + this._picNum + '.jpg'));
			}else if(num<400){	//人物头像
				this._loader.load(new URLRequest('Naruto/Images/head' + this._picNum + '.png'));
				//trace('Naruto/Images/head' + this._picNum + '.png');
			}else if(num<500){	//Credits
				this._loader.load(new URLRequest('Naruto/Images/bg' + this._picNum + '.jpg'));
			}else{				//结果画面
				this._loader.load(new URLRequest('Naruto/Images/mr' + this._picNum + '.jpg'));
			}
			this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
		}
		private function onComplete(event:Event):void{
			/*
			var image:Bitmap = Bitmap(this._loader.content);
			var bitmap:BitmapData = image.bitmapData;
			addChild(image);
			*/
			var loadedImage:Bitmap = Bitmap(this._loader.content);
			//创建新的BitmapData，把载入的图像绘入
			var bitmap:BitmapData = new BitmapData(loadedImage.width,
			loadedImage.height,true,0xffffff);		//true:打开阿尔法通道？
			bitmap.draw(loadedImage,new flash.geom.Matrix());
			//使用BitmapData创建新的Bitmap然后进行显示
			var image:Bitmap = new Bitmap(bitmap);

			if(this._picNum<300){	//场景或地图，则放在最底部
				image.x = 0;
				image.y = 0;
				this._stage.addChildAt(image,0);
				this._maskImgLoader = this._loader;	//记录场景背景的遮罩loader
			}else if(this._picNum<400){				//人物对话头像
				image.x = 20;
				image.y = 680;
				this._stage.addChild(image);
			}else if(this._picNum<500){				//Credits
				this._stage.addChild(image);
				this._menu = new Menu();
				this._menuName = this._menu.name;
				this._menu.x = 200;
				this._menu.y = 680;
				this._stage.addChild(this._menu);
			}else{		//过关画面
				image.x = 280;
				image.y = 100;
				this._stage.addChild(image);
				addButton();
			}
//			End();
		}
		/*
		private function End(){
			this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onComplete);
		}
		*/
		public function startMask(){
			//if (this.zhezhao != null && this._stage.contains(this.zhezhao)) {
			//	return;
			//}
			this.zhezhao = new Sprite();
			
			this.zhezhao.graphics.lineStyle(1, 0x000000);
			this.zhezhao.graphics.beginFill(0x888888);
			this.zhezhao.graphics.drawRect(0,0, this._stage.stageWidth + 64,this._stage.stageHeight + 64);
			this.zhezhao.graphics.endFill();
			this.zhezhao.alpha=0.5;
			this._stage.addChild(this.zhezhao);
//			this._stage.mask = this.zhezhao;
		}
		
		private function addButton(){			
			this._playAgain = new PlayAgain();
			this._playAgainName = this._playAgain.name;
			this._playAgain.x = 500;
			this._playAgain.y = 600;
			this._stage.addChild(this._playAgain);
			
			this._menu = new Menu();
			this._menuName = this._menu.name;
			this._menu.x = 630;
			this._menu.y = 600;
			this._stage.addChild(this._menu);	
		}
		
		public function checkButtonName(bName:String){
			switch(bName){
				case this._menuName:return "Menu";
				case this._playAgainName:return "playAgain";
				default:return null;
			}
		}
	}
}